package com.rm.zeyou.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rm.zeyou.entity.CityFareConfig;
import com.rm.zeyou.entity.CityFareGlobalConfig;
import com.rm.zeyou.entity.Technician;
import com.rm.zeyou.mapper.CityFareConfigMapper;
import com.rm.zeyou.mapper.CityFareGlobalConfigMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.CityFareGlobalConfigService;
import com.rm.zeyou.util.address.AddressUtil;
import com.rm.zeyou.util.address.WSSsdrAddress;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.util.gaode.GaoDeUtil;
import com.rm.zeyou.vo.response.TechnicianFreeIndexResponseVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 城市路费全局配置表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-12 15:55:07
 */
@Service
public class CityFareGlobalConfigServiceImpl extends ServiceImpl<CityFareGlobalConfigMapper, CityFareGlobalConfig> implements CityFareGlobalConfigService {
    @Resource
    private CityFareGlobalConfigMapper cityFareGlobalConfigMapper;
    @Resource
    private GaoDeUtil gaoDeUtil;
    @Resource
    private CityFareConfigMapper cityFareConfigMapper;

    @Override
    public ApiRestResponse<TechnicianFreeIndexResponseVo> technicianFreeIndex(Technician technician) throws ParseException {
        TechnicianFreeIndexResponseVo responseVo = new TechnicianFreeIndexResponseVo();
        CityFareGlobalConfig freeFareInstructions = cityFareGlobalConfigMapper.selectOne(new LambdaQueryWrapper<CityFareGlobalConfig>().select(CityFareGlobalConfig::getValue).eq(CityFareGlobalConfig::getCode, "free_fare_instructions"));
        responseVo.setFreeFareInstructions(freeFareInstructions.getValue());


        //日间起步距离
        BigDecimal dayStartDistance;
        //夜间起距离
        BigDecimal nightStartDistance;
        String  dayStartTime;
        String  dayEndTime;
        String  nightStartTime;
        String  nightEndTime;
        String address = gaoDeUtil.ReGeoCode(technician.getLongitude() + "," + technician.getLatitude());
        WSSsdrAddress wsSsdrAddress = AddressUtil.addressFormat(address);
        //城市
        String city = wsSsdrAddress.getCity();
        //省份
        String province = wsSsdrAddress.getProvince();
        //获取城市路费设置
        CityFareConfig cityFareConfig = cityFareConfigMapper.selectOneCity(city, province);
        if (CheckEmptyUtil.isEmpty(cityFareConfig)) {
            //获取全局配置
            CityFareGlobalConfig dayStartDistanceConfig = cityFareGlobalConfigMapper.selectOne(new LambdaQueryWrapper<CityFareGlobalConfig>().select(CityFareGlobalConfig::getValue).eq(CityFareGlobalConfig::getCode, "day_start_distance"));
            CityFareGlobalConfig nightStartDistanceConfig = cityFareGlobalConfigMapper.selectOne(new LambdaQueryWrapper<CityFareGlobalConfig>().select(CityFareGlobalConfig::getValue).eq(CityFareGlobalConfig::getCode, "night_start_distance"));
            CityFareGlobalConfig dayStartTimeConfig = cityFareGlobalConfigMapper.selectOne(new LambdaQueryWrapper<CityFareGlobalConfig>().select(CityFareGlobalConfig::getValue).eq(CityFareGlobalConfig::getCode, "day_start_time"));
            CityFareGlobalConfig dayEndTimeConfig = cityFareGlobalConfigMapper.selectOne(new LambdaQueryWrapper<CityFareGlobalConfig>().select(CityFareGlobalConfig::getValue).eq(CityFareGlobalConfig::getCode, "day_end_time"));
            CityFareGlobalConfig nightStartTimeConfig = cityFareGlobalConfigMapper.selectOne(new LambdaQueryWrapper<CityFareGlobalConfig>().select(CityFareGlobalConfig::getValue).eq(CityFareGlobalConfig::getCode, "night_start_time"));
            CityFareGlobalConfig nightEndTimeConfig = cityFareGlobalConfigMapper.selectOne(new LambdaQueryWrapper<CityFareGlobalConfig>().select(CityFareGlobalConfig::getValue).eq(CityFareGlobalConfig::getCode, "night_end_time"));

            dayStartDistance = new BigDecimal(dayStartDistanceConfig.getValue());
            nightStartDistance = new BigDecimal(nightStartDistanceConfig.getValue());
            dayStartTime = dayStartTimeConfig.getValue();
            dayEndTime = dayEndTimeConfig.getValue();
            nightStartTime = nightStartTimeConfig.getValue();
            nightEndTime = nightEndTimeConfig.getValue();
        } else {
            dayStartDistance = cityFareConfig.getDayStartDistance();
            nightStartDistance = cityFareConfig.getNightStartDistance();
            dayStartTime = cityFareConfig.getDayStartTime();
            dayEndTime = cityFareConfig.getDayEndTime();
            nightStartTime = cityFareConfig.getNightStartTime();
            nightEndTime = cityFareConfig.getNightEndTime();
        }

        responseVo.setDayStartTime(dayStartTime);
        responseVo.setDayEndTime(dayEndTime);
        responseVo.setNightStartTime(nightStartTime);
        responseVo.setNightEndTime(nightEndTime);
        if (!CheckEmptyUtil.isEmpty(technician.getDayFreeTolls())) {
            responseVo.setTechnicianDayStartDistance(technician.getDayFreeTolls());
        } else {
            responseVo.setTechnicianDayStartDistance(dayStartDistance);
        }
        if (!CheckEmptyUtil.isEmpty(technician.getNightFreeTolls())) {
            responseVo.setTechnicianNightStartDistance(technician.getNightFreeTolls());
        }else {
            responseVo.setTechnicianNightStartDistance(nightStartDistance);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        //判断当前时间是白天还是晚上时间
        boolean in = DateUtil.isIn(simpleDateFormat.parse(DateUtil.format(new Date(),"HH:mm")), simpleDateFormat.parse(dayStartTime), simpleDateFormat.parse(dayEndTime));
        if (in) {
            //白天时间
            responseVo.setStartDistance(dayStartDistance);
        } else {
            //晚上时间
            responseVo.setStartDistance(nightStartDistance);
        }

        return ApiRestResponse.success(responseVo);
    }

}
